package cn.xfdzx.android.apps.shop.fragment.type;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.SellingGoodsLeftLVAdapter;
import cn.xfdzx.android.apps.shop.adapter.TypeMainAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.CategoryAllBeen;
import cn.xfdzx.android.apps.shop.contract.OneCategoryContract;
import cn.xfdzx.android.apps.shop.evenbus.ClassInitDataMessage;
import cn.xfdzx.android.apps.shop.fragment.OneFragment;
import cn.xfdzx.android.apps.shop.presenter.OneCategoryPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description：新发地市场采购分类
 */
public class TypeMarketPurchaseFragment extends BaseMvpFragment<OneCategoryPresenter> implements OneCategoryContract.View{

    ListView lvTypeMenuLeft;
    RecyclerView rvTypeGoods;

    private SellingGoodsLeftLVAdapter sellingGoodsLeftLVAdapter;
    private TypeMainAdapter typeMainAdapter;
    private LinearLayoutManager linearManager;
    private String menu_name;
    private boolean is_true=true;
    @Override
    protected void initView(View view) {
        lvTypeMenuLeft=view.findViewById(R.id.lv_type_menu_left);
        rvTypeGoods=view.findViewById(R.id.rv_type_goods);

        EventBus.getDefault().register(this);
        mPresenter = new OneCategoryPresenter();
        mPresenter.attachView(this);

        sellingGoodsLeftLVAdapter = new SellingGoodsLeftLVAdapter(getActivity());
        lvTypeMenuLeft.setAdapter(sellingGoodsLeftLVAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvTypeGoods.setLayoutManager(linearLayoutManager);
        typeMainAdapter = new TypeMainAdapter(getActivity(),2);

        rvTypeGoods.setAdapter(typeMainAdapter);
        linearManager = (LinearLayoutManager) rvTypeGoods.getLayoutManager();

        mPresenter.initData("2");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_type_market_purchase;
    }

    @Override
    public void showLoading() {
        if (TYPE_IS_RETAIL == 1) {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()){
            ProgressDialog.getInstance().show(getActivity());
        }}
    }

    @Override
    public void hideLoading() {
        if (TYPE_IS_RETAIL == 1) {
        if (ProgressDialog.getInstance().materialDialog.isShowing()){
            ProgressDialog.getInstance().dismiss();
        }}
    }


    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
    public void XXX(ClassInitDataMessage messageEvent) {
        if (messageEvent.getMessage()==1){
            if (is_true) {
                mPresenter.initData("2");
            }
        }
    }
    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onInitSuccess(CategoryAllBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE){
            if (bean.getData().size()!=0){
                is_true=false;
            menu_name=bean.getData().get(0).getName();
            sellingGoodsLeftLVAdapter.setData(bean.getData());
            lvTypeMenuLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    sellingGoodsLeftLVAdapter.setSelectItem(i);
                    sellingGoodsLeftLVAdapter.notifyDataSetChanged();
                    rvTypeGoods.scrollToPosition(i);
                    //第一
                    linearManager.scrollToPositionWithOffset(i, 0);
                }
            });
            typeMainAdapter.setData(bean.getData());
            rvTypeGoods.addOnScrollListener(new RecyclerView.OnScrollListener() {
                private int scrollState;

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    this.scrollState = newState;
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                        return;
                    }
                    //获取最后一个可见view的位置
                    int lastItemPosition = linearManager.findLastVisibleItemPosition();
                    //获取第一个可见view的位置
                    int firstItemPosition = linearManager.findFirstVisibleItemPosition();
                    if (!bean.getData().get(firstItemPosition).getName().equals(menu_name)) {
                        menu_name = bean.getData().get(firstItemPosition).getName();
                        sellingGoodsLeftLVAdapter.setSelectItem(firstItemPosition);
                        sellingGoodsLeftLVAdapter.notifyDataSetInvalidated();
                    }
                }
            });
        }}else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }



    public static TypeMarketPurchaseFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
//        bundle.putString("item", "UserPostFragment");
//        bundle.putLong(Constants.UID, userUid);
        TypeMarketPurchaseFragment fragment = new TypeMarketPurchaseFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public void onDestroy() {
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

}
